//
//  ChiledViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/17.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChiledViewController.h"
#import "FindCell.h"
#import "HBHFindDetailViewController.h"
@interface ChiledViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,assign)NSInteger PageNumber;

@end

@implementation ChiledViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self getFindInfoBeanList];
}

-(void)creatView
{
    _PageNumber = 1;
    _dataArr = [NSMutableArray new];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,44, SCREENW, SCREENH-44-64) style:UITableViewStylePlain];
        _tableV.backgroundColor = backColor;
        _tableV.rowHeight = 175*screenW_6;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _PageNumber = 1;
            [_dataArr removeAllObjects];
            [_tableV.mj_footer resetNoMoreData];
            [self getFindInfoBeanList];
        }];
        _tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            _PageNumber++;
            [self getFindInfoBeanList];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115*screenW_6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindCell"];
    if (cell == nil) {
        cell = [[FindCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FindCell"];
    }
    //加载cell
    [cell setDataDic:_dataArr[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.navigationController pushViewController:[CompanyDetailViewController new] animated:YES];
    HBHFindDetailViewController *vc = [HBHFindDetailViewController new];
    vc.detailId = _dataArr[indexPath.row][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getFindInfoBeanList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/find/findInfoBeanList" Dic:@{@"pageNo":[NSString stringWithFormat:@"%ld",_PageNumber],@"pageSize":@"10",@"twoKinds.id":_twoKindsid,@"oneKinds.id":_oneKindsid,@"adCode":[standfault objectForKey:@"cityID"]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    if (_dataArr.count < [responseDic[@"count"] integerValue]) {
                        [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [HudHelper hideHudToView:self.view];
                [_tableV reloadData];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if (_dataArr.count >= [responseDic[@"count"] integerValue]) {
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableV.mj_footer endRefreshing];
                    }
                }
            });
        }];
    });
}

@end
